package com.sparsearr.datastructures.itheima.shuangseqiu;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Random;
import java.util.Scanner;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/4/19 17:48
 * @Description: 双色球
 * @Version: 1.0
 */

public class SSQ {
    private static final Logger logger = LogManager.getLogger(SSQ.class);

    public static void main(String[] args) {
        int[] arr = createNumber();

        System.out.println("===============");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+ " ");
        }
        System.out.println("===============");

        int[] userArr = userInputNumber();
        int redCount = 0;
        int buleCount = 0;
        for (int i = 0; i < userArr.length - 1; i++) {
            int redNumber = userArr[i];
            for (int j = 0; j < arr.length; j++) {
                if (redNumber == arr[i]) {
                    redCount++;
                    break;
                }
            }
        }
        int blueNumber = userArr[userArr.length - 1];
        if (blueNumber == arr[arr.length - 1]) {
            buleCount++;
        }
        logger.info("红球中奖个数：{}", redCount);
        logger.info("蓝球中奖个数：{}", buleCount);

        if (redCount == 6 && buleCount == 1){
            logger.info("恭喜你，中奖了！！！");
        } else {
            logger.info("很遗憾，没有中奖");
        }
    }

    private static int[] userInputNumber() {
        int[] arr = new int[7];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 6; ) {
            System.out.println("请输入第" + (i + 1) + "个红球号码");
            int redNumber = sc.nextInt();
            if (redNumber >= 1 && redNumber <= 33) {
                boolean flag = contains(arr, redNumber);
                if (!flag) {
                    arr[i] = redNumber;
                    i++;
                } else {
                    System.out.println("当前红球号码已经存在");
                }
            } else {
                System.out.println("请输入正确的红球号码");
            }
        }

        System.out.println("请输入蓝球号码");
        while (true) {
            int blueNumber = sc.nextInt();
            if (blueNumber >= 1 && blueNumber <= 16) {
                arr[arr.length - 1] = blueNumber;
                break;
            } else {
                System.out.println("请输入正确的蓝球号码");
            }
        }

        return arr;
    }

    private static int[] createNumber() {
        int[] arr = new int[7];
        Random r = new Random();

        for (int i = 0; i < 6; ) {
            int redNumber = r.nextInt(33) + 1;
            if (!contains(arr, redNumber)) {
                arr[i] = redNumber;
                i++;
            }
        }

        int blueNumber = r.nextInt(16) + 1;
        arr[arr.length - 1] = blueNumber;
        return arr;
    }

    private static boolean contains(int[] arr, int number) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == number) {
                return true;
            }
        }
        return false;
    }
}
